package com.mzy.basics.jarFile;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/23 13:10
 * @Version 1.0
 */
public class Test {
    /**
     * 获取Jar包的路径
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
//        File file = new File("F:\\Programming documentation\\JAVA\\MyGit仓库\\JavaDemo\\java-basics\\src\\main\\resources\\mqtt-server-1.0-SNAPSHOT-shaded.jar");
//        JarFile jarFile = new JarFile(file);
        JarFile jarFile = new JarFile("F:\\Programming documentation\\JAVA\\MyGit仓库\\JavaDemo\\java-basics\\src\\main\\resources\\mqtt-server-1.0-SNAPSHOT-shaded.jar");
        JarEntry jarEntry = jarFile.getJarEntry("application.properties");
        JarOutputStream jos = new JarOutputStream(new FileOutputStream("F:\\Programming documentation\\JAVA\\MyGit仓库\\JavaDemo\\java-basics\\src\\main\\resources\\mqtt-server-1.0-SNAPSHOT-shaded.jar"));
        jos.putNextEntry(jarEntry);

        String data = "server.port=188\n" +
                "server.host=127.0.0.1\n" +
                "server.uuid=62b0e4a9-3f8d-406b-a6f2-024d9abbd34f";
        jos.write(data.getBytes(), 0, data.getBytes().length);
        jos.finish();
        jos.close();


//        JarEntry target=null;
//        TreeMap<String, JarEntry> byName = new TreeMap<String, JarEntry>();
//        for (Enumeration<JarEntry> e = jarFile .entries(); e.hasMoreElements(); ) {
//            JarEntry entry = e.nextElement();
//            if (entry.getName().equals("application.properties")){
//                target=entry;
//
//                JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(file));
//            }
//
//            byName.put(entry.getName(), entry);
//
//        }
//        InputStream input = jarFile.getInputStream(target);
//
//
//        Properties prop = new Properties();
//        prop.load(input);
//        String serverPort = prop.getProperty("server.port");
//        String serverHost = prop.getProperty("server.host");
//        System.out.println(serverHost+serverPort);
//
//        prop.setProperty("server.port","1885");
//
//        System.out.println(prop.getProperty("server.port"));
//        byName.forEach((k,v) ->System.out.println(k+"\nvalue:"+v));

//        Test test = new Test();
//        Class aClass = test.getClass();
//        String jarPath = getJarDir(aClass);
//        System.out.println(jarPath);
    }



    // 获取 clazz 所在 jar 包路径
    public static String getJarPath(Class clazz) {

        String filePath = null;
        URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
        try {
            // 转化为utf-8编码，支持中文
            filePath = URLDecoder.decode(url.getPath(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        File file = new File(filePath);
        filePath = file.getAbsolutePath();  //得到windows下的正确路径
        return filePath;
    }


    // 获取 clazz 所在 jar 包目录
    public static String getJarDir(Class clazz) {
        String jarPath = getJarPath(clazz);
        if (jarPath.endsWith(".jar")) {    // 可执行jar包运行的结果里包含".jar"
            // 获取jar包所在目录
            jarPath = jarPath.substring(0, jarPath.lastIndexOf("/") + 1);
        }
        return jarPath;
    }
}
